import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:good_good_study/common/index.dart';
import 'package:good_good_study/pages/index_page.dart';
import 'package:good_good_study/provider/app_provider.dart';
import 'package:provider/provider.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SplashPageState();
  }
}

class _SplashPageState extends State<SplashPage>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  @override
  void initState() {
    super.initState();
    context.read<AppProvider>().initUserLoginData();
    controller = AnimationController(
        vsync: this, duration: Duration(milliseconds: 3500));
    animation = Tween(begin: 0.24, end: 1.24).animate(controller)
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          RouteUtil.push(context, IndexPage(), isReplace: true);
        }
      });

    controller?.forward();
  }

  @override
  void dispose() {
    super.dispose();
    controller?.dispose();
  }


  @override
  Widget build(BuildContext context) {
    ScreenAdapter.getInstance().init(context);
    var width = MediaQuery.of(context).size.width;
    var height = MediaQuery.of(context).size.height;
    return FadeTransition(
      opacity: animation,
      child: Container(
        child: AspectRatio(
          aspectRatio: width / height,
          child: Image.asset(
            "images/splash.png",
            fit: BoxFit.fill,
          ),
        ),
      ),
    );
  }
}
